/*****************************************************************
Mix3D WebGL Game Engine
Copyright (c) fantasy ( fantasy.x2x@gmail.com )
Listens:
Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt
 ****************************************************************/
/**
 * @Desc    : Archive interface
 * @Author  : fantasy(fantasy.x2x@gmail.com)
 */
//////////////////////////////////////////////////////////////////
if(typeof(Mix3D) === 'undefined')
{
  Mix3D = {};
};
(function(Mix3D){
//////////////////////////////////////////////////////////////////
// stream node
Mix3D.Stream = function(name, parent)
{
  this.name = name;
  this.dataHash ={};
  this.parent = parent || undefined;
  this.children =[];
};

Mix3D.Stream.prototype.constructor = Mix3D.Stream;

Mix3D.Stream.prototype.addChild = function( name )
{
  for( var i=0, len = this.children.length; i<len; ++i)
  {
    if(name === this.children[i].name )
    {
      Mix3D.warning("the archive stream is same to other!");
      return this.children[i];
    }
  }
  var stream = new Mix3D.Stream(name, this);
  this.children.push(stream);
  return stream;
};

Mix3D.Stream.prototype.writeObject = function( obj )
{
  if(obj.className)
  {
    var name = obj._guid;
    var stream = this.addChild(name.join(''));
    stream.write('className', obj.className);
    obj.save(stream);
  }
};

Mix3D.Stream.prototype.readObject = function(obj)
{
  if(obj.className)
  {
    for( var i=0, len = this.children.length; i<len; ++i)
    {
      if(this.children[i].name === obj._guid)
      {
        obj.load(this.children[i]);
        break;
      }
    }
  }
};

Mix3D.Stream.prototype.write = function( name, value )
{
  if(this.dataHash[name] === undefined)
    this.dataHash[name] = value;
  else
  {
    Mix3D.warning("Mix3D.Stream.write: the data item is exsit!");
  }
};

Mix3D.Stream.prototype.read = function( name, outValue)
{
  var v = this.dataHash[name];
  if(outValue)outValue = v;
  return v;
};

Mix3D.Stream.prototype.parse = function( datas )
{
  for( item in datas )
  {
    var value = datas[item];
    if( typeof value === 'object' && value.constructor !== Array)
    {
      this.addChild(item).pase(datas[item]);
    }
    else
      this.dataHash[item] = data[item];
  }
};
Mix3D.Stream.prototype.clear = function()
{
  this.dataHash = {};
  var i, len=this.children.length;
  for( i=0; i<len; ++i)
  {
    this.children[i].clear();
    delete this.children[i];
  }
  this.children[i] = [];
  return this;
};
//////////////////////////////////////////////////////////////////
Mix3D.Archive = function()
{
  Mix3D.Stream.call(this, 'Root');
  this.version = '1.0';
  //this.uniqueObject = {};
};

Mix3D.protoCopy(Mix3D.Stream, Mix3D.Archive);
Mix3D.Archive.prototype.constructor = Mix3D.Archive;

Mix3D.Archive.prototype.load = function( url, callback )
{
  this.clear();
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function () 
  {
    if ( xhr.readyState === xhr.DONE ) 
    {
      if ( xhr.status === 200 || xhr.status === 0 ) 
      {
        if( xhr.responseText )
        {
          var datasMap = JSON.parse( xhr.responseText );
          this.parse(datasMap);
          if(callback)callback();
        }
        else
        {
          Mix3D.warning( "Mix3D.Archive.load: [" + url + "] seems to be unreachable or file there is empty" );
        }
        //complete
      } 
      else 
      {
        Mix3D.error( "Mix3D.Archive: Couldn't load [" + url + "] [" + xhr.status + "]" );
      }
    }
  };

  xhr.open( "GET", url, true );
  if ( xhr.overrideMimeType ) xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  xhr.setRequestHeader( "Content-Type", "text/plain" );
  xhr.send( null );
};

Mix3D.Archive.prototype.save = function( outputStr )
{
};
//////////////////////////////////////////////////////////////////
})(Mix3D);// end of function(Mix3D)